package com.cqgc.controller;

import com.cqgc.pojo.dto.GoodsDTO.AddGoodsDTO;
import com.cqgc.pojo.dto.GoodsDTO.GoodsPageQueryDTO;
import com.cqgc.pojo.dto.GoodsDTO.LikeGoods;
import com.cqgc.pojo.dto.GoodsDTO.UpdateGoodsDTO;
import com.cqgc.pojo.entity.Goods;
import com.cqgc.pojo.entity.PageResult;
import com.cqgc.pojo.entity.Result;
import com.cqgc.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : 是你
 * @time : 2024/4/16 23:21
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品接口")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    @ApiOperation("分页查询")

    public Result<PageResult> list(GoodsPageQueryDTO goodsPageQueryDTO){
        PageResult pageResult = goodsService.list(goodsPageQueryDTO);
        if (pageResult!=null) {
            return Result.success(pageResult);
        }
        return Result.error();
    }

    @GetMapping("/findByCategoryId")
    @ApiOperation("按商品分类查询")
    public Result<List<Goods>> findAll(int categoryId){
        List<Goods> list = goodsService.fingByCategoryId(categoryId);
        if (list!=null) {
            return Result.success(list);
        }
        return Result.error();
    }

//
    @PostMapping("/add")
    @ApiOperation("新增商品")
    @RequiresRoles(value = {"supplier","admin"},logical = Logical.OR)

    public Result add(@RequestBody @Validated AddGoodsDTO addGoodsDTO){
        int rows = goodsService.add(addGoodsDTO);
        if (rows!=0) {
            return Result.success();
        }
        return Result.error();
    }
//
    @PostMapping("/like")
    @ApiOperation("模糊查询")
    public Result<List<Goods>> like(@RequestBody LikeGoods likeGoods){
        List<Goods> list = goodsService.like(likeGoods);
        if (list!=null) {
            return Result.success(list);
        }
        return Result.error();
    }
    @GetMapping("/detail")
    @ApiOperation("查询商品详情")
    public Result<List<Goods>> detail(){
        List<Goods> goods = goodsService.detail();
        if (goods!=null) {
            return Result.success(goods);
        }
        return Result.error();
    }
//
    @DeleteMapping
    @ApiOperation("删除商品")
    @RequiresRoles(value = {"supplier","admin"},logical = Logical.OR)
    public Result delete(int gid){
        int rows = goodsService.delete(gid);
        if (rows!=0) {
            return Result.success();
        }
        return Result.error("删除失败");
    }
//
    @PutMapping
    @ApiOperation("修改商品")
    @RequiresRoles(value = {"supplier","admin"},logical = Logical.OR)
    public Result update(@RequestBody @Validated UpdateGoodsDTO updateGoodsDTO){
        int rows = goodsService.update(updateGoodsDTO);
        if (rows!=0) {
            return Result.success();
        }
        return Result.error("修改失败");
    }

}
